package com.hedl.studyandjava8.java8_2unit.java8_23;

import com.hedl.studyandjava8.java8_2unit.java8_23.entity.Apple;
import com.hedl.studyandjava8.java8_2unit.java8_23.entity.Color;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Big Watermelon
 * @description
 * @date 2024/02/18/21:47
 */
public class TestMain {

    /**
     * 参数固定了，如果这里改变了判断的颜色就又要重新修改代码
     * @param inventory 集合
     * @return
     */
    public static List<?> filterGreenbApples(List<?> inventory){
        if (inventory.size() == 0){
            throw new RuntimeException("集合不能为null");
        }
        List<Apple> arrList = new ArrayList<>();
        for (Object o : inventory) {
            if (o instanceof Apple){
                Apple apple = (Apple) o;
                if (apple.getColor().equals(Color.GREEN)){
                    arrList.add(apple);
                }
            }
        }
        return arrList;
    }

    /**
     * 这里改变了写法，传递了筛选的功能，传递给了参数，不容在多次修改代码了
     * @param inventory 集合
     * @param color 颜色
     * @return
     */
    public static List<?> filterRedApples(List<?> inventory,Color color){
        if (inventory.size() == 0){
            throw new RuntimeException("集合不能为null");
        }
        List<Apple> arrList = new ArrayList<>();
        for (Object o : inventory) {
            if (o instanceof Apple){
                Apple apple = (Apple) o;
                if (apple.getColor().equals(color)){
                    arrList.add(apple);
                }
            }
        }
        return arrList;
    }

    /**
     * 增加了功能，筛选重量，这时候我们想到的是重写方法，如下面这样
     * @param inventory
     * @param weight
     * @return
     */
    public static List<?> filterApplesByWeight(List<?> inventory,Integer weight){
        if (inventory.size() == 0){
            throw new RuntimeException("集合不能为null");
        }
        List<Apple> arrList = new ArrayList<>();
        for (Object o : inventory) {
            if (o instanceof Apple){
                Apple apple = (Apple) o;
                if (apple.getWeight() >= weight){
                    arrList.add(apple);
                }
            }
        }
        return arrList;
    }

    public static void main(String[] args) {
        Apple apple = new Apple();

        List<Apple> inventory = new ArrayList<>();
        inventory.add(new Apple(100,Color.RED));
        inventory.add(new Apple(12,Color.GREEN));
        inventory.add(new Apple(150,Color.RED));
        inventory.add(new Apple(50,Color.RED));
        inventory.add(new Apple(8,Color.RED));
        inventory.add(new Apple(10,Color.GREEN));
        inventory.add(new Apple(59,Color.GREEN));
        inventory.add(new Apple(160,Color.RED));
        inventory.add(new Apple(110,Color.GREEN));


        List<?> objects = filterApplesByWeight(inventory,90);
        System.out.println(objects);
    }
}
